package de.fhkl.graphics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

public class BackgroundAndChallange {

	// BildBuffer
	private FloatBuffer PicVertexBuffer;
	private ShortBuffer PicTopologyBuffer;
	private FloatBuffer PicTextureBuffer;

	// PuzzleBuffer
	private FloatBuffer PuzVertexBuffer;
	private ShortBuffer PuzTopologyBuffer;
	private FloatBuffer PuzTextureBuffer;


	float x, y;

	// Mapping Vertices fuer das Bild
	private float picturetexture[] = { 0.0f, 1.0f, // top left
			0.0f, 0.0f, // bottom left
			1.0f, 1.0f, // top right
			1.0f, 0.0f // bottom right
	};

	private float puzzletexture[] = { 0.0f, 1.0f, // top left
			0.0f, 0.0f, // bottom left
			1.0f, 1.0f, // top right
			1.0f, 0.0f // bottom right
	};

	private float geometryPicture[];
	private short topologyPicture[];

	private float geometryPuzzle[];
	private short topologyPuzzle[];


	public BackgroundAndChallange(float x, float y) {

		this.x = x;
		this.y = y;

		/*
		 * Background
		 */
		geometryPicture = new float[] { -3.5f, 5.0f, 0.0f, // v0
				-3.5f, -5.0f, 0.0f, // v2
				3.5f, 5.0f, 0.0f, // v1
				3.5f, -5.0f, 0.0f // v3
		};

		topologyPicture = new short[] { 0, 1, 2, // v0, v2, v1
				3, 2, 1 }; // v3, v1, v2

		ByteBuffer picVertexBuffer = ByteBuffer
				.allocateDirect(geometryPicture.length * 4);
		picVertexBuffer.order(ByteOrder.nativeOrder());
		PicVertexBuffer = picVertexBuffer.asFloatBuffer();
		PicVertexBuffer.put(geometryPicture);
		PicVertexBuffer.position(0);

		ByteBuffer picTopologyBB = ByteBuffer
				.allocateDirect(topologyPicture.length * 12);
		picTopologyBB.order(ByteOrder.nativeOrder());
		PicTopologyBuffer = picTopologyBB.asShortBuffer();
		PicTopologyBuffer.put(topologyPicture);
		PicTopologyBuffer.position(0);

		// pictureTexturbuffer
		picVertexBuffer = ByteBuffer.allocateDirect(picturetexture.length * 32);
		picVertexBuffer.order(ByteOrder.nativeOrder());
		PicTextureBuffer = picVertexBuffer.asFloatBuffer();
		PicTextureBuffer.put(picturetexture);
		PicTextureBuffer.position(0);

		/*
		 * Das nachzubildende Puzzle
		 */
		geometryPuzzle = new float[] { -1.2f, -2.0f, 0.1f, // v0
				-1.2f, -4.0f, 0.1f, // v2
				1.2f, -2.0f, 0.1f, // v1
				1.2f, -4.0f, 0.1f // v3
		};

		topologyPuzzle = new short[] { 0, 1, 2, // v0, v2, v1
				3, 2, 1 }; // v3, v1, v2

		ByteBuffer puzVertexBB = ByteBuffer
				.allocateDirect(geometryPicture.length * 4);
		puzVertexBB.order(ByteOrder.nativeOrder());
		PuzVertexBuffer = puzVertexBB.asFloatBuffer();
		PuzVertexBuffer.put(geometryPuzzle);
		PuzVertexBuffer.position(0);

		ByteBuffer puzTopologyBB = ByteBuffer
				.allocateDirect(topologyPicture.length * 12);
		puzTopologyBB.order(ByteOrder.nativeOrder());
		PuzTopologyBuffer = puzTopologyBB.asShortBuffer();
		PuzTopologyBuffer.put(topologyPuzzle);
		PuzTopologyBuffer.position(0);

		// pictureTexturbuffer
		puzVertexBB = ByteBuffer.allocateDirect(picturetexture.length * 32);
		puzVertexBB.order(ByteOrder.nativeOrder());
		PuzTextureBuffer = puzVertexBB.asFloatBuffer();
		PuzTextureBuffer.put(puzzletexture);
		PuzTextureBuffer.position(0);

	}

	/*
	 * Textur
	 */
	public void init(GL10 gl, Context context) {
		loadGLTexture(gl, context);
	}

	private void loadGLTexture(GL10 gl, Context context) {
	}

	public void draw(GL10 gl) {

		gl.glPushMatrix();

		Textures tex = Textures.getInstance();

		// draw background
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, tex.getTexture(6)); // Textur
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Textur
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, PicVertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, PicTextureBuffer); // Textur
		gl.glDrawElements(GL10.GL_TRIANGLES, PicTopologyBuffer.limit(),
				GL10.GL_UNSIGNED_SHORT, PicTopologyBuffer);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Textur
		gl.glDisable(GL10.GL_TEXTURE_2D);

		// draw puzzlevorlage
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, tex.getTexture(7)); // Textur
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Textur
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, PuzVertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, PuzTextureBuffer); // Textur
		gl.glDrawElements(GL10.GL_TRIANGLES, PuzTopologyBuffer.limit(),
				GL10.GL_UNSIGNED_SHORT, PuzTopologyBuffer);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Textur
		gl.glDisable(GL10.GL_TEXTURE_2D);

		gl.glPopMatrix();
	}
}